package com.shaozi.nc.quartz.base;

import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.lib.execpool.base.JobExecute;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.manage.CompanyInfoManager;

public abstract class BaseCompanyJobExecute implements JobExecute {

	private Logger logger = Logger.getLogger(BaseCompanyJobExecute.class);
	@Autowired
	private CompanyInfoManager companyInfoManager;

	public BaseCompanyJobExecute() {

	}

	@Override
	public void execute() {
		Set<String> companyIds = getCompanyIds();
		if (companyIds.isEmpty()) {
			return;
		}
//		String companyId = "10004";
		for (String companyId : companyIds) {
			try {
				handleCompany(companyId);
			} catch (Exception e) {
				logger.error(SzException.getStackMessage(e));
			}
		}
		logger.debug(getClass().getSimpleName() + " finish");
	}
	

	protected abstract void handleCompany(String companyId) throws SzException;

	private Set<String> getCompanyIds() {
		long wait = System.currentTimeMillis();
		Set<String> companyIds = companyInfoManager.getCompanyIdsWaitStartup();
		if (System.currentTimeMillis() - wait > 400) {
			logger.warn("get companyIds wait to long:" + wait);
		}
		if (companyIds.isEmpty()) {
			logger.warn("not get companyId");
		}
		return companyIds;
	}
}
